﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data;
using SaleWebsite.library;
using System.Web.Mvc;

namespace SaleWebsite.Models
{
    public class Donvitinh
    {
        public ulong Id{get;set;}
        public string Name{get;set;}
        public ulong Insert()
        {

            try
            {
                return MySqlImplement.GetInstance().ExcuteQuery(Params.GetAllParams(this), "donvitinh_insert");
            }
            catch (System.Exception ex)
            {
                throw ex;
            }

        }
        public static DataSet GetAll()
        {
            return MySqlImplement.GetInstance().GetQuery("donvitinh_getall");
        }
        public int Delete()
        {
            return MySqlImplement.GetInstance().ExcuteCommand(Params.GetAllParams(this), "donvitinh_delete");
        }
        public int Save()
        {
            try
            {
                return MySqlImplement.GetInstance().ExcuteCommand(Params.GetAllParams(this), "donvitinh_update");
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        public DataSet GetGroupById()
        {
            return MySqlImplement.GetInstance().GetQuery("group_get_id", Params.GetAllParams(this));
        }
        private static Donvitinh Convert_one(DataRow row)
        {
            return new Donvitinh()

            {
                Id = ulong.Parse(row["id"].ToString()),
                Name=row["name"].ToString()
            };
        }
        public static IEnumerable<Donvitinh> ConvertAll(DataSet data)
        {
            foreach (DataRow row in data.Tables[0].Rows)
            {
                yield return Convert_one(row);
            }
        }
        private static SelectListItem GetOneSelect(DataRow row,ulong id)
        {
            return new SelectListItem()
            {

                Value=row["id"].ToString(),
                Text=row["name"].ToString(),
                Selected=ulong.Parse(row["id"].ToString())==id?true:false

            };
        }
        public static IEnumerable<SelectListItem>GetAllSelect(ulong id)
        {
            foreach (DataRow row in GetAll().Tables[0].Rows)
            {
                yield return GetOneSelect(row, id);
            }
        }
    }
}